<?php

use Drupal\shusy_category\Entity\ShusyCategory;
use Drupal\shusy_point_product\Entity\ShusyPointProduct;
use Drupal\shusy_point_product\Entity\ShusyPointProductOrder;
use Drupal\shusy_point_product\Entity\ShusyPointProductStock;

function shusy_point_product_shusy_point_product_entity_form($form, &$form_state, ShusyPointProduct $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_point_product') . '/shusy_point_product.forms.inc';
  $form_state[ShusyPointProduct::class] = $e;
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('产品标题'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $options = [];
  $categories = shusy_category_load_multiple(FALSE, ['status' => 1]);
  foreach ($categories as $category) {
    if ($category instanceof ShusyCategory) {
      $options[$category->id] = $category->name;
    }
  }
  $form['category_id'] = [
    '#options' => $options,
    '#type' => 'select',
    '#title' => t('分类'),
    '#required' => TRUE,
    '#default_value' => $e->category_id,
    '#chosen' => TRUE,
  ];
  $form['point'] = [
    '#type' => 'textfield',
    '#title' => t('所需积分'),
    '#required' => TRUE,
    '#default_value' => $e->point,
  ];
  $form['sort'] = [
    '#type' => 'textfield',
    '#title' => t('排序'),
    '#required' => TRUE,
    '#default_value' => $e->sort,
    '#element_validate' => ['element_validate_integer'],
  ];
  $form['weight'] = [
    '#type' => 'textfield',
    '#title' => t('重量'),
    '#required' => TRUE,
    '#default_value' => $e->weight,
  ];
  $form['unit'] = [
    '#title' => t('重量单位'),
    '#default_value' => $e->unit,
    '#type' => 'select',
    '#options' => $e->getUnitOptions(),
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  $form['stock'] = [
    '#type' => 'fieldset',
    '#title' => t('库存'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  ];
  $form['stock']['stock'] = [
    '#type' => 'textfield',
    '#title' => t('库存数量'),
    '#required' => TRUE,
    '#default_value' => $e->stock(),
  ];
  $form['stock']['stock_refresh'] = [
    '#type' => 'checkbox',
    '#title' => t('刷新库存'),
    '#default_value' => 0,
    '#description' => t('勾选<b>刷新库存</b>保存时库存数量才会更新.'),
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('上架'),
    '#default_value' => $e->status,
  ];
  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_point_product_shusy_point_product_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPointProduct::class];
  if ($e instanceof ShusyPointProduct) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
    $stocks = shusy_point_product_stock_load_multiple(FALSE, ['point_product_id' => $e->id]);
    if (!empty($stocks)) {
      $stock = reset($stocks);
    }
    else {
      $stock = new ShusyPointProductStock();
      $stock->point_product_id = $e->id;
    }
    if ($stock instanceof ShusyPointProductStock) {
      if ($form_state['values']['stock']['stock_refresh'] == 1) {
        $stock->stock = $form_state['values']['stock']['stock'];
        $stock->save();
      }
    }
  }
}

function shusy_point_product_shusy_point_product_delete_form($form, &$form_state, ShusyPointProduct $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_point_product') . '/shusy_point_product.forms.inc';
  $form_state[ShusyPointProduct::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_point_product/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_point_product_shusy_point_product_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPointProduct::class];
  if ($e instanceof ShusyPointProduct) {
    $e->delete();
  }
}

function shusy_point_product_shusy_point_product_order_entity_form($form, &$form_state, ShusyPointProductOrder $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_point_product') . '/shusy_point_product.forms.inc';
  $form_state[ShusyPointProductOrder::class] = $e;


  $form['express_type_id'] = [
    '#type' => 'select',
    '#title' => t('快递类型'),
    '#options' => shusy_express_type_options(),
    '#default_value' => $e->express_type_id,
    '#chosen' => TRUE,
  ];
  $form['express_number'] = [
    '#type' => 'textfield',
    '#title' => t('发货单号'),
    '#maxlength' => 32,
    '#default_value' => $e->express_number,
  ];

  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_point_product_shusy_point_product_order_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPointProductOrder::class];
  if ($e instanceof ShusyPointProductOrder) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_point_product_shusy_point_product_order_delete_form($form, &$form_state, ShusyPointProductOrder $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_point_product') . '/shusy_point_product.forms.inc';
  $form_state[ShusyPointProductOrder::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_point_product_order/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_point_product_shusy_point_product_order_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPointProductOrder::class];
  if ($e instanceof ShusyPointProductOrder) {
    $e->delete();
  }
}

function shusy_point_product_shusy_point_product_stock_entity_form($form, &$form_state, ShusyPointProductStock $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_point_product') . '/shusy_point_product.forms.inc';
  $form_state[ShusyPointProductStock::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_point_product_shusy_point_product_stock_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPointProductStock::class];
  if ($e instanceof ShusyPointProductStock) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_point_product_shusy_point_product_stock_delete_form($form, &$form_state, ShusyPointProductStock $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_point_product') . '/shusy_point_product.forms.inc';
  $form_state[ShusyPointProductStock::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_point_product_stock/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_point_product_shusy_point_product_stock_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPointProductStock::class];
  if ($e instanceof ShusyPointProductStock) {
    $e->delete();
  }
}

